package com.bvgol.examples.myAnnotation.controller;

import com.alibaba.fastjson.JSONObject;
import com.bvgol.examples.myAnnotation.ann.ApiOperateLog;
import com.bvgol.examples.myAnnotation.constants.LogConst;
import com.bvgol.examples.myAnnotation.dto.Page;
import com.bvgol.examples.myAnnotation.service.AopLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;

@Slf4j
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private AopLogService aopLogService;

    /**
     * 功能描述: get
     *
     * @param id
     * @return: java.lang.Object
     * @author: 郭辰
     * @date: 2021/5/12 11:13
     */
    @ApiOperateLog(logName = "查询日志", logType = LogConst.LogType.LogType_SELECT, logModule = LogConst.LogModule.LOG_USER_INFO)
    @GetMapping("/{id}")
    public Object selectById(@PathVariable("id") int id) {
        log.info("进入方法,方法开始执行");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("离开方法,方法执行结束");
        return Instant.now();
    }

    /**
     * 功能描述: post
     *
     * @param page
     * @return: java.lang.Object
     * @author: 郭辰
     * @date: 2021/5/12 11:13
     */
    @ApiOperateLog(logName = "查询日志", logType = LogConst.LogType.LogType_SELECT, logModule = LogConst.LogModule.LOG_USER_INFO)
    @PostMapping("/page")
    public Object page(@RequestBody Page page) {
        log.info("进入方法,方法开始执行");
        try {
            String s = JSONObject.toJSONString(page);
            log.info(s);
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("离开方法,方法执行结束");
        return Instant.now();
    }

    @ApiOperateLog(logName = "查询日志", logType = LogConst.LogType.LogType_SELECT, logModule = LogConst.LogModule.LOG_USER_INFO)
    @PostMapping("/pageInService")
    public Object pageInService(@RequestBody Page page) {
        log.info("进入方法,方法开始执行");
        String s1 = "";
        try {
            String s = JSONObject.toJSONString(page);
            log.info(s);
            Thread.sleep(3000);
            s1 = aopLogService.handleArgs(page);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("离开方法,方法执行结束");
        return Instant.now() + "----->" + s1;
    }

    /**
     * 功能描述: 方法报错并且抛出去了
     *
     * @param page
     * @return: java.lang.Object
     * @author: 郭辰
     * @date: 2021/5/12 11:13
     */
    @ApiOperateLog(logName = "查询日志", logType = LogConst.LogType.LogType_SELECT, logModule = LogConst.LogModule.LOG_USER_INFO)
    @PostMapping("/exception")
    public Object exception(@RequestBody Page page) throws Exception {
        log.info("进入方法,方法开始执行");
        try {
            String s = JSONObject.toJSONString(page);
            log.info("获取的参数:{}", s);
            Thread.sleep(3000);
            throw new Exception("就是想报错");
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    @ApiOperateLog(logName = "查询日志", logType = LogConst.LogType.LogType_SELECT, logModule = LogConst.LogModule.LOG_USER_INFO)
    @PostMapping("/exceptionNoThrow")
    public Object exceptionNoThrow(@RequestBody Page page) {
        log.info("进入方法,方法开始执行");
        try {
            String s = JSONObject.toJSONString(page);
            log.info("获取的参数:{}", s);
            Thread.sleep(3000);
            int a = 10 / 0;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Instant.now();
    }

}
